package first.program;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class CodeScreen extends Activity implements OnClickListener{
    
	private static final int feedbackConstant = 3;
	public static final String PREFS_NAME = "MyPrefsFile"; // Shared Preference File
	final Verify checkrow = new Verify();
	
	Button activebutton = null; // Initializes Button object.
	
	int pwLength = 4; // Length of the password
	String passcode[]= new String[pwLength];
	Boolean rand_setting;
	
	String activePosition [] =  {"1", "2", "3", "4"};
	//passcode will the passcode in strings and pass into the gameScreen activity.
	Button [] position = new Button[4]; // This array is used to store the buttons in ascending 
	// position order. It is used in the boolean function noWhites, which determines if all of the positions
	// have been set.
	int active_position = 1; // Sets the active position to one because it needs to
	// find the xml strings in the format of posX where x is the active position. X >= 1.
	

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.codescreen);        
    	
        SharedPreferences settings= getSharedPreferences(PREFS_NAME, 0);
   	 	rand_setting = settings.getBoolean("rand", false);	
   	
        //Sets the active button to the first position.
	    activebutton = (Button) findViewById(R.id.pos1);
	
	    for (int i= 0; i < 4; i++)
 	   {
	    
	    //Creates the string for the xml ID.
 		String posID ="pos"+(i+1);
 		//Get the memory reference for the xml ID.
 		int resPID = getResources().getIdentifier(posID, "id", "first.program");
 		//Set pos1 to first button, pos2 the second button...etc.
 		position[i] = ((Button) findViewById(resPID));
 		//Sets the button to listen.
 		position[i].setOnClickListener(this);
 	
 
 		  
 	   }    
 
	    //Activates the color buttons.
	    Button set_green = (Button) findViewById(R.id.set_green);
	    set_green.setOnClickListener(this);
	    Button set_orange = (Button) findViewById(R.id.set_orange);
	    set_orange.setOnClickListener(this);	    
	    Button set_black = (Button) findViewById(R.id.set_black);
	    set_black.setOnClickListener(this);	   
	    Button set_blue = (Button) findViewById(R.id.set_blue);
	    set_blue.setOnClickListener(this);     	    
	    Button set_red = (Button) findViewById(R.id.set_red);
	    set_red.setOnClickListener(this);	
	    Button set_purple= (Button) findViewById(R.id.set_purple);
	    set_purple.setOnClickListener(this);	    
	    Button set_yellow = (Button) findViewById(R.id.set_yellow);
	    set_yellow.setOnClickListener(this);	   
	    Button set_gray = (Button) findViewById(R.id.set_gray);
	    set_gray.setOnClickListener(this);   	    
	    Button submit = (Button) findViewById(R.id.submit);  
	    submit.setOnClickListener(this);
	  
    }

    public void onClick(View v){
       v.performHapticFeedback(feedbackConstant, 0);
 	   switch(v.getId()){
 	   
 	   case R.id.set_red:    		   
 		   activebutton.setBackgroundResource(R.drawable.circle_red);
 		   activebutton.setContentDescription("red");
 		   active_position = (active_position%4) + 1;
 		   changeActiveButton(active_position);

 	   break;
 	   
 	   case R.id.set_black:
 		  activebutton.setBackgroundResource(R.drawable.circle_black);    		  
 		   activebutton.setContentDescription("black");
 		   active_position = (active_position%4) + 1;
 		   changeActiveButton(active_position);   
 	   break;
 	   
 	   case R.id.set_blue:
 		   activebutton.setBackgroundResource(R.drawable.circle_blue); 
 		   activebutton.setContentDescription("blue");
 		   active_position = (active_position%4) + 1;
 		   changeActiveButton(active_position);
 	   break;
 	   
 	   case R.id.set_gray:
 		   activebutton.setBackgroundResource(R.drawable.circle_gray);    		  
 		   activebutton.setContentDescription("gray");
 		   active_position = (active_position%4) + 1;
 		   changeActiveButton(active_position);
 	   break;
 	   
 	   case R.id.set_green:
 		   activebutton.setBackgroundResource(R.drawable.circle_green);    		  
 		   activebutton.setContentDescription("green");
 		   active_position = (active_position%4) + 1;
 		   changeActiveButton(active_position);
 	   break;
 	   
 	   case R.id.set_orange:
 		   activebutton.setBackgroundResource(R.drawable.circle_orange);    		  
 		   activebutton.setContentDescription("orange");
 		   active_position = (active_position%4) + 1;
 		   changeActiveButton(active_position);

 	   break;
 	   
 	   case R.id.set_purple:
 		   activebutton.setBackgroundResource(R.drawable.circle_purple);    		  
 		   activebutton.setContentDescription("purple");
 		   active_position = (active_position%4) + 1;
 		   changeActiveButton(active_position);
 		  
 	   break;
 	   
 	   case R.id.set_yellow:
 		   activebutton.setBackgroundResource(R.drawable.circle_yellow);    		  
 		   activebutton.setContentDescription("yellow");
 		   active_position = (active_position%4) + 1;
 		   changeActiveButton(active_position);
 	   break;
 	   
 	   case R.id.submit:
 		   
 		   
		//Checks to ensure the user has chosen a color for every position.
	 	if(checkrow.noWhites(position))
	 	{
	 		
	 		if (rand_setting)
	 		{		   
		 		for(int i = 0; i < 4; i++)
		 			{
		 				Log.i("cs", "i is "+i);
		 				//Store the passcode in an array.
		 				passcode[i] = position[i].getContentDescription().toString();

		 			}
		 		//The Passcode array is passed into the next activity. With the key "passcode"
		 		Intent myIntent = new Intent(v.getContext(), GameScreen.class);
		 	    myIntent.putExtra("passcode", passcode);
		 	    myIntent.putExtra("mode", 0);
		 		startActivity(myIntent); 
	 		}
	 		else
	 		{ 			
	 			if(checkrow.noRepeats(position))
	 			{
 			   
	 			  for(int i = 0; i < 4; i++)
	 			  {
	 				 Log.i("cs", "i is "+i);
	 				 //Store the passcode in an array.
	 				 passcode[i] = position[i].getContentDescription().toString();

	 			  }
 			   
	 			  //The Passcode array is passed into the next activity. With the key "passcode"
	 			  Intent myIntent = new Intent(v.getContext(), GameScreen.class);
	 	          myIntent.putExtra("passcode", passcode);
	 	          myIntent.putExtra("mode", 0);
	 			  startActivity(myIntent); 
	 			}
	 			else
	 			{
	 				Context context = getApplicationContext();
	 				CharSequence text ="No Repeats Allowed!";
	 				int duration = Toast.LENGTH_SHORT;
	 				Toast toast = Toast.makeText (context, text, duration);
	 				toast.show();  
	 			}
	 		}
	 	}
 		else
		{
			//Error message if user does not select a color for 1 or more spots.
			Context context = getApplicationContext();
			CharSequence text ="Please select a color for any remaining spots.";
			int duration = Toast.LENGTH_SHORT;
			Toast toast = Toast.makeText (context, text, duration);
			toast.show(); 
		}   
 		   break;
 		   
 		   
 		   
 	   default:

 		   activebutton = (Button) findViewById(v.getId());
 		   for(int i = 0; i < 4; i++){ 
 			  String temp = activePosition[i];
 			   if(activebutton.getTag().toString().equals(temp)){
 				   active_position = (i%4) + 1; 
 				   changeActiveButton(active_position);
 			   }
 		   }
 		   
 		   break;
 
 	   
 	   }
    }

	private void changeActiveButton(int actpos) {

	      String aID = "pos"+actpos; 
	      int aresID = getResources().getIdentifier(aID, "id", "first.program");
	      activebutton = ((Button) findViewById(aresID));	
		
	}
  
   	@Override
   	public void onBackPressed(){
   	return;	
   	}

}